import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    FinancialCostServiceProxy,
    FinancialCostOfDeviceEditDto,
    CreateOrUpdateFinancialCostOfDeviceInput,
    ComboboxItemDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { CalendarModule } from 'primeng/calendar';
import { SelectItem } from 'primeng/api';

@Component({
    selector: 'createOrEditFinancialCostModal',
    templateUrl: './create-or-edit-financial-cost-modal.component.html'
})
export class CreateOrEditFinancialCostModalComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _fnancialCostServiceProxy: FinancialCostServiceProxy) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    //editDto: CostOfDeviceEditDto;
    //  extensions: Array<EditDtoExtensionsItem>;
    optionAll;
    editDto: FinancialCostOfDeviceEditDto = new FinancialCostOfDeviceEditDto();
    DeviceIdentifyType: ComboboxItemDto[];
    IdentifyType: SelectItem[];
    deviceTypes: ComboboxItemDto[];
    deviceType: SelectItem[];
    ngOnInit(): void {}
    show(id?: number): void {
        this.active = true;
        this._fnancialCostServiceProxy.getForEditAsync(id).subscribe(result => {
            this.editDto = result.financialCostOfDevice;
            this.deviceTypes = result.deviceTypeItems;
            const self = this;

            this.deviceType = [];
            for (let j = 0; j < this.deviceTypes.length; j++) {
                this.deviceType.push({
                    value: Number(self.deviceTypes[j].value),
                    label: self.deviceTypes[j].displayText
                });
            }

            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let input = new CreateOrUpdateFinancialCostOfDeviceInput();
        input.financialCostOfDevice = this.editDto;

        this._fnancialCostServiceProxy
            .createOrUpdateAsync(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                    console.log(input);
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
